﻿Imports DTO
Imports BUS
Public Class ucKhoHang

    Dim kh As New KhoHangDTO

    Private Sub barbtnThemKhoHang_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnThemKhoHang.ItemClick
        Dim frm As New frmThemKhoHang
        AddHandler frm.LamMoiKhoHKhiThem, AddressOf LayDSKhoHang
        frm.ShowDialog()
    End Sub

    Sub LayDSKhoHang()
        GCKhohang.DataSource = KhoHangBUS.LayDSKhoHang()
    End Sub

    Private Sub ucKhoHang_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        barbtnSuaChuaKhoHang.Enabled = False
        barbtnXoaKhoHang.Enabled = False
        barbtnXuatKhoHang.Enabled = False
        LayDSKhoHang()
    End Sub

    Private Sub barbtnSuaChuaKhoHang_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnSuaChuaKhoHang.ItemClick
        Dim frm As New frmThemKhoHang
        frm.kt = 1
        frm.makh = kh.MaKhoHang
        frm.tenkh = kh.TenKhoHang
        frm.nlh = kh.NguoiLienHe
        frm.dc = kh.DiaChi
        frm.dt = kh.DienGia
        frm.kh = kh.KyHieu
        frm.dg = kh.DienGia
        frm.matt = kh.MaTrangThai
        frm.Text = "Sửa chữa kho hàng"
        frm.ShowDialog()
    End Sub

    Private Sub barbtnXoaKhoHang_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnXoaKhoHang.ItemClick
        Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 20)
        If pq.Xoa = True Then
            Try
                If KhoHangBUS.XoaKhoHang(kh) Then
                    DevExpress.XtraEditors.XtraMessageBox.Show("Xóa thành công kho hàng", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    LayDSKhoHang()
                    HeThongBUS.themNhatKy(frmMain.maND, "Quản lý kho hàng", "Xóa", kh.MaKhoHang)
                End If
            Catch ex As Exception
                DevExpress.XtraEditors.XtraMessageBox.Show("Xóa thất bại, kho hàng này đang được sử dụng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                KhoHangBUS.DongKetNoi.Close()
            End Try
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub barbtnNapLaiKhoHang_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnNapLaiKhoHang.ItemClick
        LayDSKhoHang()
    End Sub

    Private Sub GridView1_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick
        If e.RowHandle >= 0 Then
            barbtnSuaChuaKhoHang.Enabled = True
            barbtnXoaKhoHang.Enabled = True
            kh.MaKhoHang = GridView1.GetRowCellValue(e.RowHandle, GCMaKhoHang).ToString()
            kh.TenKhoHang = GridView1.GetRowCellValue(e.RowHandle, GCTenKhoHang).ToString()
            kh.NguoiLienHe = GridView1.GetRowCellValue(e.RowHandle, GCLienHeKhoHang).ToString()
            kh.DiaChi = GridView1.GetRowCellValue(e.RowHandle, GCDiaChiKhoHang).ToString()
            kh.DienThoai = GridView1.GetRowCellValue(e.RowHandle, GCDienThoaiKhoHang).ToString()
            kh.KyHieu = GridView1.GetRowCellValue(e.RowHandle, GCKyHieuKhoHang).ToString()
            kh.DienGia = GridView1.GetRowCellValue(e.RowHandle, GCGhiChuKhoHang).ToString()
            kh.MaTrangThai = Convert.ToInt32(GridView1.GetRowCellValue(e.RowHandle, GCConQuanLyNCC))
        End If
    End Sub

    Private Sub GridView1_CustomDrawRowIndicator(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs) Handles GridView1.CustomDrawRowIndicator
        If e.RowHandle >= 0 Then
            If e.Info.IsRowIndicator Then
                e.Info.DisplayText = (e.RowHandle + 1).ToString()
            End If
        End If
    End Sub

    Private Sub GridView1_RowStyle(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs) Handles GridView1.RowStyle
        If (e.RowHandle >= 0) Then
            Dim category As Integer = Convert.ToInt32(GridView1.GetRowCellValue(e.RowHandle, GCConQuanLyNCC))
            If category = 0 Then
                e.Appearance.ForeColor = Color.Red
                e.Appearance.FontStyleDelta = FontStyle.Italic
            End If
        End If
    End Sub
End Class
